package com.my;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by YJH on 2017/7/5 9:53.
 */
public class CheckCodeServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取请求参数：CHECK_CODE_PARAM_NAME
        String paramCode = request.getParameter("CHECK_CODE_PARAM_NAME");

        //2、获取session中的 CHECK_CODE_KEY 属性值
        String sessionCode = (String) request.getSession().getAttribute("CHECK_CODE_KEY");
        System.out.println("paramCode = " + paramCode + "\nsessionCode = " + sessionCode);

        //3、对比，判断是否一致，若一致，通过，不一致，不通过
        if (!(paramCode != null && paramCode.equals(sessionCode))) {
            request.getSession().setAttribute("message","验证码不一致!");
            response.sendRedirect(request.getContextPath()+"/check/index.jsp");
            return;
        }

        System.out.println("受理请求！！！");
    }
}
